// pages/choice-coupon/choice-coupon.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_price: '',
    couponList: [],
    money: '',
    coupon_name: '',
    coupon_id: '',
    coupon_type: '',
    manjian_money: '',
    discount: '',
    status: ''
  },
  updataRadio_peisong_type(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    this.setData({
      select: index,
      coupon_id: e.currentTarget.dataset.id, //优惠券id
      money: e.currentTarget.dataset.money, //优惠券优惠金额
      discount: e.currentTarget.dataset.discount, //优惠券折扣
      manjian_money: e.currentTarget.dataset.manjian_money, //优惠券满减进额
      coupon_type: e.currentTarget.dataset.coupon_type //优惠券类型
    })
  },
  get_couponList() {
    let that = this;
    let data = {
      order_price: that.data.order_price,
    }
    common.request(api.get_couponList, data, 'post').then((res) => {
      if (res.status == 1) {
        console.log(res)
        var couponList = res.couponList
        for (let i = 0; i < couponList.length; i++) {
          that.setData({
            coupon_type: couponList[i].coupon_type
          })
        }
        that.setData({
          couponList: res.couponList
        })
      }
    })
  },

  //确认选择
  choice_coupon() {
    let that = this;
    if (that.data.status == 1) {
      wx.showToast({
        title: '优惠卷只能使用一次',
        icon: 'none'
      })
      return false
    }
    if (that.data.status == 2) {
      wx.showToast({
        title: '优惠券已过期',
        icon: 'none',
      })
      return false
    }
    if (that.data.coupon_type == 2 &&  that.data.order_price <that.data.manjian_money) {
      wx.showToast({
        title: '无法使用该满减券',
        icon: 'none'
      })
      return false
    }
    var pages = getCurrentPages(); // 获取页面栈
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      mydata_coupon: {
        coupon_id: that.data.coupon_id, //优惠券id
        money: that.data.money, //优惠金额
        discount: that.data.discount, // 优惠券折扣
        coupon_type: that.data.coupon_type //优惠券类型
      } // 假数据
    })
    wx.navigateBack({
      delta: 1
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.order_price)
    this.setData({
      order_price: options.order_price
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_couponList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})